package com.hrserviceserve.controller;


import com.hrserviceserve.entity.Result;
import com.hrserviceserve.entity.Salary;
import com.hrserviceserve.service.SalaryService;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "工资接口")
@RequestMapping("/salary")
public class SalaryController {
    @Autowired
    SalaryService salaryService;

    @GetMapping

    public Result findAll(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,
                          @RequestParam(value = "personnelId",required = false) Integer personnelId
    ){
            Map<String, Object> map = new HashMap<>();
            map.put("page", page);
            map.put("limit", limit);
            map.put("personnelId", personnelId);
            return salaryService.findByPage(map);
    }


    @PostMapping
    public Result save(@RequestBody Salary salary){
        Integer sum=(salary.getBaseSalary()+salary.getRewardSalary()+salary.getSubsidy());
        if(sum<=5000){
            sum=sum;
        }else if(sum>5000&&sum<=12000) {
            sum=Integer.valueOf((int)(sum-(sum-5000)*0.1)-210);
        }else if(sum>12000&&sum<=25000) {
            sum=Integer.valueOf((int)(sum-(sum-12000)*0.2)-1410);
        }else if(sum>25000&&sum<=35000) {
            sum=Integer.valueOf((int)(sum-(sum-25000)*0.25)-2660);
        }else sum=Integer.valueOf((int)(sum-(sum-35000)*0.3)-4410);
        salary.setSum(sum);
//        salary.setSum(salary.getBaseSalary()+salary.getRewardSalary()+salary.getSubsidy());
        return salaryService.save(salary);
    }
    @DeleteMapping
    public Result deleteById(@RequestParam("id") int id){
        int delete = salaryService.delete(id);
        if (delete>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
}
